clear
set more off
set matsize 2000
set scheme lean2

*	Change CD
*	global root         	"..."
	global source			="$root/raw"
	global temp				="$root/temp"
	global code				="$root/code"
	global output			="$root/output"

	
********************************************************************************
import excel "$temp/parish.xlsx", clear firstrow
		
replace kommun 		= subinstr(kommun, " ", "", .)

gen s_largefarms 	= (farmown_d + farmten_d) / farmunits_1900

gen popdensity 		= pop1900 / area

gen s_noble 		= Noble / (Noble + Crown + Tax)
	
save "$temp/sweden_part", replace
	
********************************************************************************
import excel "$temp/moriczinequalitydata.xlsx", clear firstrow
rename County county
keep kommun county toppagrarianfyrk toppfyrk
save "$temp/sweden_tax", replace


********************************************************************************
import excel "$source/sweden_soilsuitability.xlsx", clear firstrow
keep ref_code avg_suitability
collapse (mean) avg_suitability, by(ref_code)
save "$temp/sweden_iv", replace
	

	
********************************************************************************
import excel "$temp/electiondata.xlsx", clear firstrow
rename County county

		
foreach var in 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 {
	rename V`var' v`var'
}
		
*1911
gen	conservative_e1911 = v14/v10
gen	conservative_v1911 = v14/v12

gen	liberal_e1911 = v15/v10
gen	liberal_v1911 = v15/v12
	
gen	social_e1911 = v16/v10
gen	social_v1911 = v16/v12
	
gen	turnout1911 = v11/v10
	
*1914 (mar)
gen	conservative_e1914 = v22/v18
gen	conservative_v1914 = v22/v20

gen	liberal_e1914 = v23/v18
gen	liberal_v1914 = v23/v20
	
gen	social_e1914 = v24/v18
gen	social_v1914 = v24/v20
	
gen	turnout1914 = v19/v18

*1914 (sep)
gen	conservative_e1915 = v30/v26
gen	conservative_v1915 = v30/v28

gen	liberal_e1915 = v31/v26
gen	liberal_v1915 = v31/v28
	
gen	social_e1915 = v32/v26
gen	social_v1915 = v32/v28
	
gen	turnout1915 = v27/v26
	
	
*1917
gen	conservative_e1917 = v38/v34
gen	conservative_v1917 = v38/v36
	
gen	agri_e1917=v39/v34
gen	agri_v1917=v39/v36
	
gen	liberal_e1917 = v40/v34
gen	liberal_v1917 = v40/v36
	
gen	social_e1917 = v41/v34
gen	social_v1917 = v41/v36
	
gen	othersocial_e1917 = v42/v34
gen	othersocial_v1917 = v42/v36

gen	turnout1917 = v35/v34

*1920
gen	conservative_e1920 = v51/v43
gen	conservative_v1920 = v51/v47
	
gen	agri_e1920=v52/v43
gen	agri_v1920=v52/v47
	
gen	liberal_e1920 = (v53+v54)/v43
gen	liberal_v1920 = (v53+v54)/v47
		
gen	social_e1920 = v55/v43
gen	social_v1920 = v55/v47
	
gen	othersocial_e1920 = v56/v43
gen	othersocial_v1920 = v56/v47

gen	turnout1920 = v46/v43
	
*1921
gen	conservative_e1921 = v66/v58
gen	conservative_v1921 = v66/v62
	
gen	agri_e1921=v67/v58
gen	agri_v1921=v67/v62
	
gen	liberal_e1921 = (v68+v69)/v58
gen	liberal_v1921 = (v68+v69)/v62
		
gen	social_e1921 = v70/v58
gen	social_v1921 = v70/v62
	
gen	othersocial_e1921 = v71/v58
gen	othersocial_v1921 = v71/v62

gen	turnout1921 = v61/v58
	
*1924
gen	conservative_e1924 = v80/v72
gen	conservative_v1924 = v80/v76
	
gen	agri_e1924=v81/v72
gen	agri_v1924=v81/v76
	
gen	liberal_e1924 = (v82+v83)/v72
gen	liberal_v1924 = (v82+v83)/v76
		
gen	social_e1924 = v84/v72
gen	social_v1924 = v84/v76
	
gen	othersocial_e1924 = (v85+v86)/v72
gen	othersocial_v1924 = (v85+v86)/v76

gen	turnout1924 = v75/v72
	
*1928
gen	conservative_e1928 = v95/v87
gen	conservative_v1928 = v95/v91
	
gen	agri_e1928=v96/v87
gen	agri_v1928=v96/v91
	
gen	liberal_e1928 = (v97+v98)/v87
gen	liberal_v1928 = (v97+v98)/v91
	
gen	social_e1928 = v99/v87
gen	social_v1928 = v99/v91
	
gen	othersocial_e1928 = v100/v87
gen	othersocial_v1928 = v100/v91

gen	turnout1928 = v90/v87

*1932
gen	conservative_e1932 = v109/v101
gen	conservative_v1932 = v109/v105
	
gen	agri_e1932=v110/v101
gen	agri_v1932=v110/v105
	
gen	liberal_e1932 = (v111+v112)/v101
gen	liberal_v1932 = (v111+v112)/v105
	
gen	social_e1932 = v113/v101
gen	social_v1932 = v113/v105
	
gen	othersocial_e1932 = v114/v101
gen	othersocial_v1932 = v114/v105

gen	turnout1932 = v104/v101
	
*1936
gen	conservative_e1936 = v126/v118
gen	conservative_v1936 = v126/v122
	
gen	agri_e1936=v127/v118
gen	agri_v1936=v127/v122
	
gen	liberal_e1936 = v128/v118
gen	liberal_v1936 = v128/v122
	
gen	social_e1936 = v129/v118
gen	social_v1936 = v129/v122
	
gen	othersocial_e1936 = (v130+v131)/v118
gen	othersocial_v1936 = (v130+v131)/v122

gen	nazi_e1936 = v132/v118
gen	nazi_v1936 = v132/v122

gen	turnout1936 = v121/v118

*1940
gen	conservative_e1940 = v142/v134
gen	conservative_v1940 = v142/v138
	
gen	agri_e1940=v143/v134
gen	agri_v1940=v143/v138
	
gen	liberal_e1940 = v144/v134
gen	liberal_v1940 = v144/v138
	
gen	social_e1940 = v145/v134
gen	social_v1940 = v145/v138
	
gen	othersocial_e1940 = (v146+v147)/v134
gen	othersocial_v1940 = (v146+v147)/v138

gen	turnout1940 = v137/v134
	
*1944
gen	conservative_e1944 = v156/v148
gen	conservative_v1944 = v156/v152
	
gen	agri_e1944=v157/v148
gen	agri_v1944=v157/v152
	
gen	liberal_e1944 = v158/v148
gen	liberal_v1944 = v158/v152
	
gen	social_e1944 = v159/v148
gen	social_v1944 = v159/v152
	
gen	othersocial_e1944 = v160/v148
gen	othersocial_v1944 = v160/v152

gen	turnout1944 = v151/v148
	
	
replace kommun = subinstr(kommun, " ", "", .)

replace county="Gbg och Bohus" if county=="Göteborg och Bohus"
replace county="Uppland" if county=="Uppsala" 
	
drop v*


reshape long conservative_e conservative_v agri_e agri_v liberal_e liberal_v social_e social_v othersocial_e othersocial_v nazi_e nazi_v turnout, i(kommun county) j(year)

gen left_e = social_e + othersocial_e
	replace left_e = social_e if othersocial_e==.
gen left_v = social_v + othersocial_v
	replace left_v = social_v if othersocial_v==.
	
foreach var in conservative_v agri_v  liberal_v left_v  nazi_v {
	replace `var'=0 if `var'==.
}
gen test = conservative_v + agri_v + liberal_v + left_v + nazi_v
	
save "$temp/swedish_elections", replace
